package demo7;

public class SellTicketTest implements Runnable {
    int ticket = 100;
    Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            sell();
        }
    }


//    public synchronized void sell() {
    public void sell() {
        synchronized (obj) {   //锁住代码块
            if (ticket > 0) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":正在卖" + ticket + "票");
                ticket--;
            }
        }
        }



}
